package com.eqdd.yiqidian.ui.gongneng.renshi.gonggao;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.TrumpetListResultBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.ui.mine.renwu.TextActivity;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.TimeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 吕志豪 on 17-10-14  上午11:01.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class ShowTrumpetListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private SlimAdapterEx<TrumpetListResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "小喇叭列表");
    }

    @Override
    public void initData() {

        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> refresh());
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_129, new SlimInjector<TrumpetListResultBean>() {
            @Override
            public void onInject(TrumpetListResultBean data, IViewInjector injector) {

                injector.text(R.id.tv_content, data.getContent())
                        .text(R.id.tv_date, TimeUtil.formatWebTime(data.getCreateTime()));
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        dataBinding.coolRefreshView.setRefreshing(true);
        dataBinding.recyclerView.setSwipeItemClickListener((view, pos) -> {
            Bundle bundle = new Bundle();
            bundle.putString(Config.TITLE, "小喇叭");
            bundle.putString(Config.CONTENT, slimAdapterEx.getDataItem(pos).getContent());
            startActivity(TextActivity.class, bundle);
        });
    }

    @Override
    public void setView() {

    }

    private void refresh() {
        OkGo.<HttpResult<List<TrumpetListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.TRUMPET_LIST)
                .params("userGuid", user.getGuid())
                .params("comid", user.getCompanyId())
                .execute(new JsonCallBack<HttpResult<List<TrumpetListResultBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<TrumpetListResultBean>>> response) {
                        HttpResult<List<TrumpetListResultBean>> httpResult = response.body();
                        dataBinding.coolRefreshView.setRefreshing(false);
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }

                    @Override
                    public void onError(Response<HttpResult<List<TrumpetListResultBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }
                });
    }
}
